package org.wang.generalcode.modules.sys.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wang.generalcode.modules.sys.pojo.entity.Menu;
import org.wang.generalcode.modules.sys.pojo.model.Option;
import org.wang.generalcode.modules.sys.pojo.params.MenuParams;
import org.wang.generalcode.modules.sys.pojo.params.PageParams;
import org.wang.generalcode.modules.sys.service.IMenuService;
import org.wang.generalcode.modules.sys.pojo.vo.MenuVO;
import org.wang.generalcode.modules.sys.pojo.vo.RouteVO;
import org.wang.generalcode.support.mybaties.Condition;
import org.wang.generalcode.support.result.Result;
import org.wang.generalcode.support.utils.Func;

import javax.validation.Valid;
import java.util.List;

/**
 * 菜单管理 控制器
 *
 * @author wz
 * @since 2024-08-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/menu")
@Schema(description = "菜单管理", name = "菜单管理接口")
public class MenuController {

	private final IMenuService menuService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Schema(description = "详情", example = "传入menu")
	public Result<Menu> detail(Menu menu) {
		Menu detail = menuService.getOne(Condition.getQueryWrapper(menu));
		return Result.success(detail);
	}


	/**
	 *  菜单管理列表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 3)
	@Schema(description = "菜单管理列表", example = "传入menu")
	public Result<List<MenuVO>> page(PageParams menu) {
		List<MenuVO> pages = menuService.listMenus(menu);
		return Result.success(pages);
	}

	/**
	 * 新增 菜单管理
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Schema(description = "新增", example = "传入menu")
	public Result save(@Valid @RequestBody MenuParams menuParams) {
		return Result.judge(menuService.saveMenu(menuParams));
	}

	/**
	 * 修改 菜单管理
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Schema(description = "修改", example = "传入menu")
	public Result update(@Valid @RequestBody MenuParams menuParams) {
		return Result.judge(menuService.saveMenu(menuParams));
	}



	/**
	 * 删除 菜单管理
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@Schema(description = "删除", example = "传入ids")
	public Result remove(@Parameter(name = "主键集合", required = true) @RequestParam String ids) {
		return Result.judge(menuService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 菜单路由列表
	 * @return
	 */
	@GetMapping("/routes")
	@ApiOperationSupport(order = 3)
	@Schema(description = "菜单路由列表", example = "菜单路由列表")
	public Result<List<RouteVO>> listRoutes() {
//		Set<String> roles = SecurityUtils.getRoles();
		List<String> roles =StpUtil.getRoleList(StpUtil.getLoginId());
		List<RouteVO> routeList = menuService.listRoutes(roles);
		return Result.success(routeList);
	}


	/**
	 * 菜单下拉列表
	 * @return
	 */
	@GetMapping("/options")
	@ApiOperationSupport(order = 3)
	@Schema(description = "菜单下拉列表", example = "菜单下拉列表")
	public Result<List<Option>> listMenuOptions(@RequestParam(required = false, defaultValue = "false") boolean onlyParent) {
		List<Option> menus = menuService.listMenuOptions(onlyParent);
		return Result.success(menus);
	}

}
